package org.microser.gataway.zuul.system.controller;

import io.swagger.annotations.Api;
import org.microser.gataway.zuul.contants.DefConstant;
import org.microser.gataway.zuul.system.entity.Permission;
import org.microser.gataway.zuul.system.entity.Role;
import org.microser.gataway.zuul.system.service.PermissionService;
import org.microser.gataway.zuul.system.service.RoleService;
import org.microser.gataway.zuul.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author liujiamiao
 * @since 2020-11-25
 */
@RestController
@RequestMapping("/system/permission")
@Api(tags = "权限接口")
public class PermissionController {

    @Autowired
    private PermissionService permissionService;

    @Autowired
    private RoleService roleService;

    @Autowired
    private RedisTemplate redisTemplate;

    @GetMapping("/getPermissionList")
    public R getPermission(HttpServletRequest request){
        String uuid = request.getHeader(DefConstant.UUID_TOKEN);
        String token = String.valueOf(redisTemplate.opsForValue().get(uuid));
        if(StringUtils.isEmpty(token)){
            return R.errorCustom();
        }
        //角色
        List<Role> roles = roleService.queryRoleListByUserId("1");
        //权限列表
        List<Permission> permissions = permissionService.permissions(roles);
        //权限值
        List<String> permissionValue = permissionService.permissionValue(permissions);

        return R.ok().
                data("permissions",permissions).
                data("permissionValue",permissionValue);
    }

}

